/**
 * @Description: ${引入所需依赖}
 * @Param Vue最后注册全局对象  axios请求后台核心  qs参数格式化  Connect系统请求接口ip或者域名配置
 * @Return
 * @Author 焦政
 * @Date 2018/11/4
 * @Version 1.0
 */
import Vue from 'vue'
import axios from 'axios'
import {Connect, DeBug} from './url'


let qs = require('qs')
const baseURL = Connect.baseURL

/**
 * @Description: ${控制台输出请求的结果}
 * @Param
 * @Return
 * @Author 焦政
 * @Date 2018/11/4
 * @Version 1.0
 */
let showResult = (link, re) => {
    if (DeBug) {
        console.log(`接口:${link}请求成功,返回结果`, re)
    }
}

/**
 * @Description: ${请求报错处理}
 * @Param
 * @Return
 * @Author 焦政
 * @Date 2018/11/4
 * @Version 1.0
 */
let e_error = (link, e) => {
    if (DeBug) {
        console.log(`接口:${link}请求出错,错误信息:`, e)
    }
}

/**
 * @Description: ${post formdata请求}
 * @Param
 * @Return
 * @Author 焦政
 * @Date 2018/11/4
 * @Version 1.0
 */
function afp(link, setting) {
    let formPost = axios.create({
        baseURL,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
        },
        method: 'post',
    })
    formPost.interceptors.response.use(
        function (response) {
            let re = response.data
            showResult(link, re)
            return re
        },
        function (error) {
            e_error(link, error)
        },
    )
    let s = qs.stringify(setting)
    return formPost.post(link, s)
}

/**
 * @Description: ${post formdata携带token请求}
 * @Param
 * @Return
 * @Author 焦政
 * @Date 2018/11/4
 * @Version 1.0
 */
function afpByToken(link, setting) {
    let formTokenPost = axios.create({
        baseURL,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
        },
        method: 'post',
    })

// 请求拦截 塞入Token
    formTokenPost.interceptors.request.use(config => {
        // console.log( 'token',localStorage.getItem('accessToken'));

        if (localStorage.getItem('accessToken')) {
            config.headers.token = localStorage.getItem('accessToken')
        }
        return config
    })

// 响应拦截
    formTokenPost.interceptors.response.use(
        function (response) {
            let re = response.data
            showResult(link, re)
            return re
        },
        function (error) {
            e_error(link, error)
        },
    )
    let s = qs.stringify(setting)
    return formTokenPost.post(link, s)
}

/**
 * @Description: ${post json请求}
 * @Param
 * @Return
 * @Author 焦政
 * @Date 2018/11/4
 * @Version 1.0
 */
function ajp(link, setting) {
    let jpost = axios.create({
        baseURL,
        headers: {
            'Content-Type': 'application/json;charset=UTF-8',
        },
        method: 'post',
    })

    jpost.interceptors.response.use(
        function (response) {
            let re = response.data
            showResult(link, re)
            return re
        },
        function (error) {
            e_error(link, error)
        },
    )
    return jpost.post(link, setting)
}

/**
 * @Description: ${post json携带token}
 * @Param
 * @Return
 * @Author 焦政
 * @Date 2018/11/4
 * @Version 1.0
 */
function ajpByToken(link, setting) {
    let jToken = axios.create({
        baseURL,
        headers: {
            'Content-Type': 'application/json;charset=UTF-8',
        },
        method: 'post',
    })

// 请求拦截 塞入Token
    jToken.interceptors.request.use(config => {
        if (localStorage.getItem('accessToken')) {
            config.headers.token = localStorage.getItem('accessToken')
        }
        return config
    })

    jToken.interceptors.response.use(
        function (response) {
            let re = response.data
            showResult(link, re)
            return re
        },
        function (error) {
            e_error(link, error)
        },
    )

    showResult(setting)
    return jToken.post(link, setting)
}

/**
 * @Description: ${get json请求}
 * @Param
 * @Return
 * @Author 焦政
 * @Date 2018/11/4
 * @Version 1.0
 */
function ajg(link, setting) {
    let GetPost = axios.create({
        baseURL,
        headers: {
            'Content-Type': 'application/json;charset=UTF-8',
        },
        method: 'get',
    })

    GetPost.interceptors.response.use(
        function (response) {
            let re = response.data
            showResult(re)
            return re
        },
        function (error) {
            e_error(link, error)
        },
    )

    let s = qs.stringify(setting)
    return GetPost.get(link, s)
}

/**
 * @Description: ${get json携带token}
 * @Param
 * @Return
 * @Author 焦政
 * @Date 2018/11/4
 * @Version 1.0
 */
function ajgByToken(link, setting) {
    let GetToken = axios.create({
        baseURL,
        headers: {
            'Content-Type': 'application/json;charset=UTF-8',
        },
        method: 'get',
    })

// 请求拦截 塞入Token
    GetToken.interceptors.request.use(config => {
        if (localStorage.getItem('accessToken')) {
            config.headers.token = localStorage.getItem('accessToken')
        }
        return config
    })

    GetToken.interceptors.response.use(
        function (response) {
            let re = response.data
            showResult(link, re)
            return re
        },
        function (error) {
            e_error(link, error)
        },
    )

    let s = qs.stringify(setting)
    return GetToken.get(link, s)
}

/**
 * @Description: ${全局对象}
 * @Param
 * @Return
 * @Author 焦政
 * @Date 2018/11/4
 * @Version 1.0
 */
const JZAxios = {
    //post formdata参数请求
    axios_formPost: {
        afp,
        afpByToken,
    },
//    post json参数请求
    axios_jsonPost: {
        ajp,
        ajpByToken,
    },
//    get json请求
    axios_jsonGet: {
        ajg,
        ajgByToken,
    },
//    delete 请求
    axios_delete: {}
}

Vue.prototype.$JZAxios = JZAxios;
